package com.oro.converters;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import org.springframework.web.jsf.FacesContextUtils;

import com.oro.backingbeans.InserisciScuolaBean;
import com.oro.domain.Sede;

public class SedeStringConverter implements Converter {

	private List<Sede> sedi;

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) throws ConverterException {

		InserisciScuolaBean inserisciScuolaBean = (InserisciScuolaBean) FacesContextUtils
				.getWebApplicationContext(context).getBean(
						"inserisciScuolaBean");

		sedi = inserisciScuolaBean.getSchoolInput().getBranches();

		for (Sede s : sedi) {
			if (s.toString().equals(value))
				return s;
		}
		return null;

	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) throws ConverterException {

		if (value != null)
			return ((Sede) value).toString();
		else
			return null;

	}

	public List<Sede> getSedi() {
		return sedi;
	}

	public void setSedi(List<Sede> sedi) {
		this.sedi = sedi;
	}

}
